<template>
  <el-dialog
    v-el-drag-dialog
    title="上传电子合同"
    :visible.sync="visible"
    width="700px"
    append-to-body
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :model="temp" :rules="rules" label-width="120px" label-position="right" style="height: 200px;">
      <el-form-item label="上传电子合同:" prop="fileList">
        <el-upload
          class="avatar-uploader"
          action="#"
          :file-list="temp.fileList"
          :show-file-list="true"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-change="handleChange"
          :on-exceed="handleExceed"
          :limit="1"
          :auto-upload="false"
          accept=".pdf"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">(注：电子合同仅支持上传PDF，上传后覆盖原电子合同)</div>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.sumbit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { uploadDepositContract } from '@/api'
  export default {
    name: 'UploadContract',
    data() {
      return {
        visible: false,
        imageUrl: '',
        dialogVisible: false,
        dialogImageUrl: '',
        temp: {
          fileList: []
        },
        rules: {
          fileList: [{ required: true, message: '上传文件不能为空', trigger: ['change', 'blur'] }]
        }
      }
    },
    methods: {
      open(temp) {
        this.temp = {
          ...this.temp,
          ...temp
        }
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
        })
      },
      reset() {
        this.temp = {
          fileList: []
        }
      },
      handleChange(file, fileList) {
        this.temp.fileList = fileList
        const isAllowedType = ['application/pdf'].includes(file.raw.type)
        const isAllowedSize = file.size / 1024 / 1024 < 10 // 小于10MB
        if (!isAllowedType) {
          const currIdx = this.temp.fileList.indexOf(file)
          this.temp.fileList.splice(currIdx, 1)
          return this.$message.error('只能上传PDF文件!')
        } else if (!isAllowedSize) {
          const currIdx = this.temp.fileList.indexOf(file)
          this.temp.fileList.splice(currIdx, 1)
          return this.$message.error('上传的w文件大小不能超过10MB!')
        }
      },
      handleRemove(file) {
        const currIdx = this.temp.fileList.indexOf(file)
        this.temp.fileList.splice(currIdx, 1)
      },
      handlePreview(e) {
        console.log('handlePreview', e)
      },
      handleExceed(file) {
        this.$message.error('只支持上传一个附件!')
      },
      success() {
        this.$refs.dataForm.validate(async valid => {
          if (!valid) {
            return
          }
          this.sumbit()
        })
      },
      async sumbit() {
        await uploadDepositContract({
          depositCode: this.temp.depositCode,
          file: this.temp.fileList[0].raw
        })
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '上传成功',
          type: 'success',
          duration: 2000
        })
      }
    }
  }
</script>

<style lang="scss">
.avatar-uploader-wrap {
  display: inline-block;
}
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  width: 178px;
  height: 178px;
  font-size: 28px;
  line-height: 178px;
  color: #8c939d;
  text-align: center;
}
.avatar {
  display: block;
  width: 178px;
  height: 178px;
}
.el-upload__tip {
  margin-top: 0;
  font-size: 14px;
}
.upload-actions {
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: space-around;
  width: 100%;
  height: 100%;
  font-size: 20px;
  color: #fff;
  text-align: center;
  cursor: default;
  background-color: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity 0.3s;
  &:hover {
    opacity: 1;
  }
  i {
    cursor: pointer;
  }
}
</style>
